package format;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * 
 */

/**
 * @author bocar
 *
 */
public class Collection2Array {

	/**
	 * Convert Array To List
	 * 
	 * @param objs
	 * @return
	 */
	public List<Object> array2List(Object[] objs) {
		if (objs == null || objs.length == 0) {
			return new ArrayList<Object>();
		}
		return Arrays.asList(objs);
	}

	/**
	 * Convert List To Array
	 * 
	 * @param lists
	 * @return
	 */
	public Object[] list2Array(List<Object> lists) {
		if (lists == null || lists.size() == 0) {
			return null;
		}
		return lists.toArray(new Object[lists.size()]);
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Collection2Array test = new Collection2Array();

		System.out.println("Convert Array To List");
		String[] strs = new String[]{"a","b","c","a","c"};
		List list = test.array2List(strs);
		for (Iterator it = list.iterator(); it.hasNext();) {
			System.out.println(it.next());
		}

		System.out.println("Convert List To Array");
		Object[] nstrs = (Object[]) test.list2Array(list);
		for (int i = 0; i < nstrs.length; i++) {
			System.out.println(nstrs[i]);
		}
	}

}
